using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 02/04/2010 11:03:48
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CMunicipios
   {
       private string IDT_MUN ;
       private string IDT_ESLOC ;
       private string MUNICIPIO_SEDE ;
       private string UF_MUN ;
       private string NOME_MUN ;

	   /// <sumary>
       /// Construtor da classe municipios
       /// <sumary>
       public CMunicipios()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Municipios
       /// <sumary>
       public CMunicipios( string IDT_MUN, string IDT_ESLOC, string MUNICIPIO_SEDE, string UF_MUN, string NOME_MUN )
       {
          this.IDT_MUN = IDT_MUN;
          this.IDT_ESLOC = IDT_ESLOC;
          this.MUNICIPIO_SEDE = MUNICIPIO_SEDE;
          this.UF_MUN = UF_MUN;
          this.NOME_MUN = NOME_MUN;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do municipio
        /// <sumary>
        public string Idt_mun 
		{
		   get
		   {
		       return this.IDT_MUN;
		   }
		   set
		   {
		       this.IDT_MUN = value;
		   }
		}
     	/// <sumary>
        /// Identificador do escritorio local
        /// <sumary>
        public string Idt_esloc 
		{
		   get
		   {
		       return this.IDT_ESLOC;
		   }
		   set
		   {
		       this.IDT_ESLOC = value;
		   }
		}
     	/// <sumary>
        /// Municipio sede: V - Sim., F - Nao
        /// <sumary>
        public string Municipio_sede 
		{
		   get
		   {
		       return this.MUNICIPIO_SEDE;
		   }
		   set
		   {
		       this.MUNICIPIO_SEDE = value;
		   }
		}
     	/// <sumary>
        /// Unidade Federativa do municipio
        /// <sumary>
        public string Uf_mun 
		{
		   get
		   {
		       return this.UF_MUN;
		   }
		   set
		   {
		       this.UF_MUN = value;
		   }
		}
     	/// <sumary>
        /// Nome do municipio
        /// <sumary>
        public string Nome_mun 
		{
		   get
		   {
		       return this.NOME_MUN;
		   }
		   set
		   {
		       this.NOME_MUN = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Municipios.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_MUNICIPIOS
			                  ( IDT_MUN, IDT_ESLOC, MUNICIPIO_SEDE, UF_MUN, NOME_MUN )
						   values
						      ( :IDT_MUN, :IDT_ESLOC, :MUNICIPIO_SEDE, :UF_MUN, :NOME_MUN )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_MUN",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_ESLOC",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("MUNICIPIO_SEDE",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("UF_MUN",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("NOME_MUN",OracleType.VarChar)); 
				
                command.Parameters["IDT_MUN"].Value = this.IDT_MUN;
                command.Parameters["IDT_ESLOC"].Value = this.IDT_ESLOC;
                command.Parameters["MUNICIPIO_SEDE"].Value = this.MUNICIPIO_SEDE;
                command.Parameters["UF_MUN"].Value = this.UF_MUN;
                command.Parameters["NOME_MUN"].Value = this.NOME_MUN;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Municipios.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_MUNICIPIOS set 
			                   IDT_ESLOC = :IDT_ESLOC, MUNICIPIO_SEDE = :MUNICIPIO_SEDE, UF_MUN = :UF_MUN, NOME_MUN = :NOME_MUN
						   where
						       IDT_MUN = :IDT_MUN ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_MUN",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_ESLOC",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("MUNICIPIO_SEDE",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("UF_MUN",OracleType.Char)); 
				command.Parameters.Add(new OracleParameter("NOME_MUN",OracleType.VarChar)); 
				
                command.Parameters["IDT_MUN"].Value = this.IDT_MUN;
                command.Parameters["IDT_ESLOC"].Value = this.IDT_ESLOC;
                command.Parameters["MUNICIPIO_SEDE"].Value = this.MUNICIPIO_SEDE;
                command.Parameters["UF_MUN"].Value = this.UF_MUN;
                command.Parameters["NOME_MUN"].Value = this.NOME_MUN;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Municipios.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_MUNICIPIOS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_MUN", DbType.Int32, this.IDT_MUN));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_MUNICIPIOS order by NOME_MUN ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CMunicipios PesquisaporCodigo(string idt_mun, string login)
        {
            CMunicipios obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_MUNICIPIOS 
                            WHERE IDT_MUN = :IDT_MUN";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_MUN", DbType.String, idt_mun);
               
				qy.Executar();
                obj = new CMunicipios( qy.ObterCampo("IDT_MUN"), qy.ObterCampo("IDT_ESLOC"), qy.ObterCampo("MUNICIPIO_SEDE"), qy.ObterCampo("UF_MUN"), qy.ObterCampo("NOME_MUN") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CMunicipios();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_MUN() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
		static public DataTable ObterPorIDT_ESLOC(string idt_esloc)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_MUNICIPIOS  where IDT_ESLOC = :IDT_ESLOC order by NOME_MUN ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_ESLOC", OracleType.Number));
                command.Parameters["IDT_ESLOC"].Value = idt_esloc;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

